<template>
  <div class="common-wrap">
    <QueryForm
      ref="conditionForm"
      module="cargoForecast"
      :searchBtnLoading="loading"
      v-model:searchForm="searchForm"
      v-model:search-column="searchColumn"
      @handle-search="onSearch"
    />
    <div class="common-box">
      <el-row style="height: 40px" class="con-button-rows">
        <ButtonGroup
          :disabledDelete="multipleSelection.length != 1"
          :hasPermiAdd="['order:cargoForecast:add']"
          :hasPermiDelete="['order:cargoForecast:delete']"
          :buttonType="['add', 'delete']"
          @add="onAdd"
          @delete="onDelete"
        ></ButtonGroup>
      </el-row>
      <MyTable
        tableId="cargoForecast"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pageVo="pageVo"
        ref="commonTable"
        moduleName="cargoForecast"
        @handleSelectionChange="handleSelectionChange"
        @handle-page-change="handlePageChange"
      ></MyTable>
    </div>
    <Add
      v-if="dialogVisible"
      v-model:visible="dialogVisible"
      :currRow="currRow"
      :isType="isType"
      @onSubmit="onSearch"
    ></Add>
  </div>
</template>

<script setup lang='tsx'>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useI18n } from "vue-i18n";
import Add from "./components/add.vue";
import * as api from "@/api/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { hasPermi } from "@/utils/mUtils.js";
import { week } from "@/static/week.js";
let { t } = useI18n();

defineOptions({
  name: "CargoForecast",
});
const pageVo = reactive({
  pageSize: 50,
  total: 10,
  currentPage: 1,
  status: true,
});
const loading = ref(true);
const dataSource = ref([]);
const columns = ref([
  {
    type: "checkbox",
    width: 50,
    align: "center",
    fixed: "left",
  },
  {
    title: "序号",
    type: "seq",
    width: 60,
    fixed: "left",
  },
  {
    field: "customerCode",
    title: "客户代码",
    slots: {
      default: ({ row }) => {
        return [
          <div class="flex-table-center-between">
            <el-button
              type="primary"
              disabled={!hasPermi(["order:cargoForecast:edit"])}
              link
              onClick={() => {
                onEdit(row);
              }}
            >
              {row.customerCode || ""}
            </el-button>
            <span
              onClick={() => {
                onConfirm(row);
              }}
              v-show={!row.status && hasPermi(["order:cargoForecast:confirm"])}
              class="release-application-goods"
            >
              {t("确")}
            </span>
            <span
            v-show={!row.transferFlag && hasPermi(["order:cargoForecast:turn"])}
              onClick={() => {
                onTurn(row);
              }}
              style="color: #FF9900;
              border-color: #FF9900;"
              class="release-application-goods"
            >
              {t("转")}
            </span>
          </div>,
        ];
      },
    },
    width: 'auto',
  },
  {
    field: "status",
    title: "状态",
    formatter: ({ cellValue }) => {
      const obj = {
        0: t("已预报"),
        1: t("已确认"),
        2: t("已到仓"),
      };
      return obj[cellValue] || "";
    },
  },
  {
    field: "customerNameZh",
    title: "客户名称",
  },
  {
    field: "destWarehouseName",
    title: "目的地",
  },
  {
    field: "grossWeight",
    title: "毛重",
  },
  {
    field: "volume",
    title: "体积",
  },
  {
    field: "whetherPalletPutaway",
    title: "是否卡板入库",
  },
  {
    field: "palletQuantity",
    title: "卡板数量",
  },
  {
    field: "salesName",
    title: "销售",
  },
  {
    field: "customerServiceName",
    title: "客服",
  },
  {
    field: "createTime",
    title: "登记时间",
  },
  {
    field: "actualInventoryQuantity",
    title: "实际入仓数量",
  },
  {
    field: "week",
    title: "预报所属周",
  },
]);
const searchForm = ref({});
const searchColumn = ref([
  {
    type: "label_input",
    label: "客户名称",
    prop: "cargoForecastCode",
  },
  {
    type: "label_input",
    label: "销售",
    prop: "cargoForecastCode",
  },
  {
    type: "label_input",
    label: "客服",
    prop: "cargoForecastCode",
  },
  {
    type: "label_select",
    label: "预报所属周",
    prop: "cargoForecastCode",
    optiosn: week,
    props: {
      value: "value",
      label: "label",
    },
  },
]);
const dialogVisible = ref(false);
const multipleSelection = ref([]);
const currRow = ref({});
const isType = ref("add");
onMounted(() => {
  onSearch();
});
const list = () => {
  loading.value = true;
  api.default.order.cargoForecast
    .list({
      ...searchForm.value,
      pageNum: pageVo.currentPage,
      pageSize: pageVo.pageSize,
    })
    .then((res) => {
      dataSource.value = res.rows;
      loading.value = false;
      pageVo.total = res.total;
    })
    .catch(() => {
      loading.value = false;
    });
};
const onConfirm = (row) => {
  api.default.order.cargoForecast
    .edit({
      id: row.id,
      status: 1,
    })
    .then(() => {
      ElMessage.success(t("确认成功"));
      onSearch();
    });
};
const onTurn = (row) => {
  api.default.order.cargoForecast
    .transferOrder([row.id])
    .then(() => {
      ElMessage.success(t("转换成功"));
      onSearch();
    });
};
const onAdd = () => {
  isType.value = "add";
  if (multipleSelection.value.length != 0) {
    currRow.value = multipleSelection.value[0];
  }
  dialogVisible.value = true;
};
const onEdit = (row) => {
  isType.value = "edit";
  currRow.value = { ...row };
  dialogVisible.value = true;
};
const onDelete = () => {
  const ids = multipleSelection.value.map((item) => item.id);
  ElMessageBox.confirm(t("是否确定删除当前选择数据？"), t("提示"), {
    confirmButtonText: t("确定"),
    cancelButtonText: t("取消"),
    type: "warning",
  })
    .then(() => {
      api.default.order.cargoForecast.remove(ids).then(() => {
        ElMessage.success(t("删除成功"));
        onSearch();
      });
    })
    .catch(() => {});
};
const handleSelectionChange = (list) => {
  multipleSelection.value = list;
};
const handlePageChange = (val) => {
  const { currentPage, pageSize } = val;
  pageVo.currentPage = currentPage;
  pageVo.pageSize = pageSize;
  list();
};
const onSearch = () => {
  pageVo.currentPage = 1;
  list();
};
</script>

<style lang="scss" scoped>
.pcMenu {
  height: 100%;
  width: 100%;
  background: #fff;
}
</style>